From 6a8208c9a9037904b579ca96a6266677cdc7b6d9 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Wed, 23 Jul 2014 11:58:09 -0700 Subject: [PATCH] Ensure binaries are present for tests Also make sure that binaries are named appropriately so they don't clash. --- src/cargo/core/manifest.rs | 7 ++++--- src/cargo/util/toml.rs | 26 +++++++++++++++++++------- tests/test_cargo_test.rs | 31 +++++++++++++++++++++++++++---- 3 files changed, 50 insertions(+), 14 deletions(-) diff --git a/src/cargo/core/manifest.rs b/src/cargo/core/manifest.rs index 301831fc0..fa752fa07 100644 --- a/src/cargo/core/manifest.rs +++ b/src/cargo/core/manifest.rs @@ -331,13 +331,14 @@ impl Target { } } - pub fn bin_target(name: &str, src_path: &Path, profile: &Profile) -> Target { + pub fn bin_target(name: &str, src_path: &Path, profile: &Profile, + metadata: Option) -> Target { Target { kind: BinTarget, name: name.to_string(), src_path: src_path.clone(), profile: profile.clone(), - metadata: None + metadata: metadata, } } @@ -347,7 +348,7 @@ impl Target { name: name.to_string(), src_path: src_path.clone(), profile: profile.clone(), - metadata: None + metadata: None, } } diff --git a/src/cargo/util/toml.rs b/src/cargo/util/toml.rs index 1f9f1ffb0..45dfacb1d 100644 --- a/src/cargo/util/toml.rs +++ b/src/cargo/util/toml.rs @@ -532,22 +532,34 @@ fn normalize(libs: &[TomlLibTarget], } fn bin_targets(dst: &mut Vec, bins: &[TomlBinTarget], - dep: TestDep, default: |&TomlBinTarget| -> String) { + dep: TestDep, metadata: &Metadata, + default: |&TomlBinTarget| -> String) { for bin in bins.iter() { let path = bin.path.clone().unwrap_or_else(|| { TomlString(default(bin)) }); for profile in target_profiles(bin, dep).iter() { + let metadata = if profile.is_test() { + // Make sure that the name of this test executable doesn't + // conflicts with a library that has the same name and is + // being tested + let mut metadata = metadata.clone(); + metadata.mix(&format!("bin-{}", bin.name)); + Some(metadata) + } else { + None + }; dst.push(Target::bin_target(bin.name.as_slice(), &path.to_path(), - profile)); + profile, + metadata)); } } } fn example_targets(dst: &mut Vec, examples: &[TomlExampleTarget], - default: |&TomlExampleTarget| -> String) { + default: |&TomlExampleTarget| -> String) { for ex in examples.iter() { let path = ex.path.clone().unwrap_or_else(|| TomlString(default(ex))); @@ -566,11 +578,11 @@ fn normalize(libs: &[TomlLibTarget], TomlString(default(test)) }); - let profile = &Profile::default_test(); // make sure this metadata is different from any same-named libs. let mut metadata = metadata.clone(); - metadata.mix(&format!("test-{}", test.name.as_slice())); + metadata.mix(&format!("test-{}", test.name)); + let profile = &Profile::default_test(); dst.push(Target::test_target(test.name.as_slice(), &path.to_path(), profile, @@ -589,14 +601,14 @@ fn normalize(libs: &[TomlLibTarget], match (libs, bins) { ([_, ..], [_, ..]) => { lib_targets(&mut ret, libs, Needed, metadata); - bin_targets(&mut ret, bins, test_dep, + bin_targets(&mut ret, bins, test_dep, metadata, |bin| format!("src/bin/{}.rs", bin.name)); }, ([_, ..], []) => { lib_targets(&mut ret, libs, test_dep, metadata); }, ([], [_, ..]) => { - bin_targets(&mut ret, bins, test_dep, + bin_targets(&mut ret, bins, test_dep, metadata, |bin| format!("src/{}.rs", bin.name)); }, ([], []) => () diff --git a/tests/test_cargo_test.rs b/tests/test_cargo_test.rs index 910f0437f..1a0ff9c94 100644 --- a/tests/test_cargo_test.rs +++ b/tests/test_cargo_test.rs @@ -38,8 +38,6 @@ test!(cargo_test_simple { test test_hello ... ok\n\n\ test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured\n\n", COMPILING, p.root().display()))); - - assert_that(&p.bin("test/foo"), existing_file()); }) test!(many_similar_names { @@ -109,8 +107,6 @@ test!(cargo_test_failing_test { 0 ignored; 0 measured\n\n", COMPILING, p.root().display(), sep = path::SEP))); - - assert_that(&p.bin("test/foo"), existing_file()); }) test!(test_with_lib_dep { @@ -493,3 +489,30 @@ test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured\n\n\ compiling = COMPILING, dir = p.root().display()).as_slice())); }) + +test!(bin_there_for_integration { + let p = project("foo") + .file("Cargo.toml", r#" + [package] + name = "foo" + version = "0.0.1" + authors = [] + "#) + .file("src/main.rs", " + fn main() { std::os::set_exit_status(1); } + #[test] fn main_test() {} + ") + .file("tests/foo.rs", r#" + use std::io::Command; + #[test] + fn test_test() { + let status = Command::new("target/test/foo").status().unwrap(); + assert!(status.matches_exit_status(1)); + } + "#); + + let output = p.cargo_process("cargo-test").exec_with_output().assert(); + let output = str::from_utf8(output.output.as_slice()).assert(); + assert!(output.contains("main_test ... ok"), "no main_test\n{}", output); + assert!(output.contains("test_test ... ok"), "no test_test\n{}", output); +}) -- 2.30.2